package com.ibm.moa.outmail.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ibm.moa.outmail.MailActivity;
import com.ibm.moa.outmail.R;
import com.ibm.moa.outmail.module.MailInfo;
import com.ibm.moa.outmail.view.SlideView;
import com.ibm.moa.outmail.view.SlideView.OnSlideListener;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter implements OnSlideListener {

	protected static final String TAG = "ExpandableListViewAdapter";
	private Context context;
	private List<String> timeTitles;
	private Map<Integer, List<MailInfo>> mailInfoMap;
	private SlideView mLastSlideViewWithStatusOn;
	private DeleteMailListener deleteMailListener;

	private List<Integer> indexs = new ArrayList<Integer>();

	public ExpandableListViewAdapter(Context context, List<String> timeTitles, Map<Integer, List<MailInfo>> mailInfoMap,
			DeleteMailListener deleteMailListener) {
		super();

		this.context = context;
		this.timeTitles = timeTitles;
		this.mailInfoMap = mailInfoMap;
		this.deleteMailListener = deleteMailListener;

	}

	@Override
	public int getGroupCount() {
		return timeTitles.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		Set<Integer> keySet = mailInfoMap.keySet();
		indexs.clear();
		for (Integer integer : keySet) {
			indexs.add(integer);
		}

		Collections.sort(indexs);

		if (indexs.size() > groupPosition)
			return mailInfoMap.get(indexs.get(groupPosition)).size();
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return timeTitles.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		List<MailInfo> list = mailInfoMap.get(indexs.get(groupPosition));
		if (null != list)
			return list.get(childPosition);

		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

		convertView = View.inflate(context, R.layout.mail_list_time_tital_item, null);
		ImageView ivArrow = (ImageView) convertView.findViewById(R.id.mail_list_time_tital_arrow);
		TextView tvTitleName = (TextView) convertView.findViewById(R.id.mail_list_time_tital_tv);

		String titalName = timeTitles.get(groupPosition);
		if (null == titalName) {
			return convertView;
		}

		tvTitleName.setText(titalName);

		if (isExpanded) {
			ivArrow.setBackgroundResource(R.drawable.iv_arrow_up);
		} else {
			ivArrow.setBackgroundResource(R.drawable.iv_arrow_down);
		}

		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {

		ViewHolder holder;
		SlideView slideView = (SlideView) convertView;
		if (slideView == null) {
			View itemView = View.inflate(context, R.layout.mail_list_check_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) itemView.findViewById(R.id.mail_list_item_name_tv);
			holder.date = (TextView) itemView.findViewById(R.id.mail_list_item_date_tv);
			holder.title = (TextView) itemView.findViewById(R.id.mail_list_item_title_tv);
			holder.fujian = (ImageView) itemView.findViewById(R.id.mail_list_item_fujian_flag);

			slideView = new SlideView(context);
			// 这里把item加入到slideView
			slideView.setContentView(itemView);
			slideView.setClickable(true);
			// 下面是做一些数据缓存
			slideView.setOnSlideListener(this);
			slideView.setTag(holder);
		} else {
			holder = (ViewHolder) slideView.getTag();
		}

		final Integer groupPos = indexs.get(groupPosition);
		List<MailInfo> mailList = mailInfoMap.get(groupPos);

		if (null != mailList) {
			final MailInfo mailInfo = mailList.get(childPosition);

			if (null != mailInfo) {
				// 通过timeMillis属性分辨是本地数据，还是网络获取的数据
				final long timeMillis = mailInfo.timeMillis;
				if (timeMillis > 0) {// 本地数据库的数据
					holder.name.setText(mailInfo.recAddr);
					slideView.openDelete();// 打开左滑出现删除图标
				} else {// 网络上获取的数据
					holder.name.setText(mailInfo.mailUserId);
				}

				if (!mailInfo.readFlag) {
					holder.name.setTextColor(Color.parseColor("#FF4ca986"));
				} else {
					holder.name.setTextColor(Color.BLACK);
				}
				if (mailInfo.haveAttach) {
					holder.fujian.setVisibility(View.VISIBLE);
				} else {
					holder.fujian.setVisibility(View.GONE);
				}
				holder.title.setText(mailInfo.subject);
				holder.date.setText(mailInfo.sendDateFormat);

				// 默认不打开划出的图标
				slideView.shrink();

				// 条目点击事件
				slideView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						deleteMailListener.onItemClicked(parent, mailInfo);
					}
				});

				// 删除事件
				slideView.findViewById(R.id.rl_delete).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						deleteMailListener.deleteMail(mailInfo);
					}
				});

				// 回复
				slideView.findViewById(R.id.rl_reply).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context, MailActivity.class);
						intent.putExtra("mailType", 1);

						if (timeMillis > 0) {// 本地数据库的数据
							intent.putExtra("mailInfo", mailInfo);
							intent.putExtra("isSend", true);
						} else {// 网络上获取的数据
							intent.putExtra("mailId", mailInfo.mailId);
							intent.putExtra("getData", true);
						}

						context.startActivity(intent);
					}
				});

				// 转发
				slideView.findViewById(R.id.rl_transfer).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context, MailActivity.class);
						intent.putExtra("mailType", 3);

						if (timeMillis > 0) {// 本地数据库的数据
							intent.putExtra("mailInfo", mailInfo);
							intent.putExtra("isSend", true);
						} else {// 网络上获取的数据
							intent.putExtra("mailId", mailInfo.mailId);
							intent.putExtra("getData", true);
						}

						context.startActivity(intent);
					}
				});

				// 回复全部
				slideView.findViewById(R.id.rl_reply_all).setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context, MailActivity.class);
						intent.putExtra("mailType", 2);

						if (timeMillis > 0) {// 本地数据库的数据
							intent.putExtra("mailInfo", mailInfo);
							intent.putExtra("isSend", true);
						} else {// 网络上获取的数据
							intent.putExtra("mailId", mailInfo.mailId);
							intent.putExtra("getData", true);
						}

						context.startActivity(intent);
					}
				});
			}
		}

		return slideView;
	}

	static class ViewHolder {
		TextView name;
		TextView date;
		TextView title;
		ImageView fujian;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public void onSlide(View view, int status) {
		// 如果当前存在已经打开的SlideView，那么将其关闭
		if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
			mLastSlideViewWithStatusOn.shrink();
		}
		// 记录本次处于打开状态的view
		if (status == SLIDE_STATUS_ON) {
			mLastSlideViewWithStatusOn = (SlideView) view;
		}
	}

	@Override
	public void onClick(View view) {
	}

	public interface DeleteMailListener {
		public void deleteMail(MailInfo mailInfo);

		public void onItemClicked(View parent, MailInfo mailInfo);
	}
}
